package com.goose.bmm.controler.fragment;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Vibrator;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import com.goose.bmm.R;
import com.goose.bmm.controler.adapter.ListPlayerAdapter;
import com.goose.bmm.controler.dialog.PlayerDialog;
import com.goose.bmm.model.business.Constants;
import com.goose.bmm.model.business.DataGlobal;

public class SettingHomeTeamFragment extends Fragment implements
		OnClickListener, OnItemLongClickListener {

	private View mView;
	private Button mButtonAddPlayer;
	private EditText mEditTextTeamName;
	private EditText mEditTextCoachName;
	private ListView mListViewPlayers;

	private ListPlayerAdapter mListPlayerAdapter;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		mView = inflater.inflate(R.layout.fragment_setting_team_home,
				container, false);
		mButtonAddPlayer = (Button) mView
				.findViewById(R.id.btn_add_player_home);
		mEditTextTeamName = (EditText) mView
				.findViewById(R.id.edt_team_name_home);
		mEditTextCoachName = (EditText) mView.findViewById(R.id.edt_coach_home);
		mListViewPlayers = (ListView) mView.findViewById(R.id.lv_player_home);

		mButtonAddPlayer.setOnClickListener(this);

		mListPlayerAdapter = new ListPlayerAdapter(getActivity(),
				DataGlobal.homeListPlayer);
		mListViewPlayers.setAdapter(mListPlayerAdapter);
		mListViewPlayers.setOnItemLongClickListener(this);

		return mView;
	}
	
	@Override
	public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
			final int position, long arg3) {
		Vibrator vibrator = (Vibrator) getActivity().getSystemService(
				Context.VIBRATOR_SERVICE);
		vibrator.vibrate(500);
		new AlertDialog.Builder(getActivity())
				.setMessage(
						getResources().getString(
								R.string.dialog_massage_what_do_you_want))
				.setPositiveButton(getResources().getString(R.string.edit),
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								dialog.cancel();
								dialog.dismiss();
								showEditPlayerDialog(position);
							}
						})
				.setNegativeButton(getResources().getString(R.string.cancel),
						null)
				.setNeutralButton(getResources().getString(R.string.delete),
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								showDeletePlayerDialog(position);
							}
						}).show();
		return true;
	}

	@Override
	public void onClick(View v) {
		if (v.getId() == R.id.btn_add_player_home) {

			PlayerDialog addPlayerDialog = new PlayerDialog(
					SettingHomeTeamFragment.this, null, getActivity(),
					Constants.ADD_PLAYER_DIALOG);
			addPlayerDialog.show();
		}
	}

	public void refresfListView() {
		mListViewPlayers.invalidateViews();
	}

	private void showEditPlayerDialog(int position) {
		PlayerDialog editPlayerDialog = new PlayerDialog(
				SettingHomeTeamFragment.this, null, getActivity(),
				Constants.EDIT_PLAYER_DIALOG);
		editPlayerDialog.setupInfo(position);
		editPlayerDialog.show();
	}

	private void showDeletePlayerDialog(final int position) {
		new AlertDialog.Builder(getActivity())
				.setMessage(
						getResources().getString(
								R.string.dialog_massage_are_you_want_delete)
								+ " " + (position + 1) + " ?")
				.setPositiveButton(getResources().getString(R.string.ok),
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								DataGlobal.homeListPlayer.remove(position);
								refresfListView();
							}
						})
				.setNegativeButton(getResources().getString(R.string.no), null)
				.show();
	}

	public EditText getmEditTextTeamName() {
		return mEditTextTeamName;
	}

	public EditText getmEditTextCoachName() {
		return mEditTextCoachName;
	}

	public ListView getmListViewPlayers() {
		return mListViewPlayers;
	}

	public ListPlayerAdapter getmListPlayerAdapter() {
		return mListPlayerAdapter;
	}

}
